//
//  FortuneDetailViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/17.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "FortuneDetailViewController.h"

@interface FortuneDetailViewController ()

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)BlurImageView *blurImage;
@property (nonatomic, strong)FortuneTitleView *titleView;
@property (nonatomic, assign)CGPoint point;
@property (nonatomic, strong)NSString *urlToday;
@property (nonatomic, strong)NSString *urlWeek;

@property (nonatomic, strong)FortuneDetailModel *Dmodel;
@property (nonatomic, strong)FortuneDetailView *fortuneView;
//tableView的加载
@property (nonatomic, strong)NSArray *arr;

@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong)UIActivityIndicatorView *act;
@property (nonatomic, strong)UIAlertView *alert;
@property (nonatomic, strong)NSArray *webArr;
@property (nonatomic, strong)NSArray *dateArr;
@end

@implementation FortuneDetailViewController
@synthesize isResult;
- (void)viewDidLoad {
    [super viewDidLoad];
    //当天时间
    NSDate *ToDay = [NSDate date];
    NSString *newDate = [NSString stringWithFormat:@"%@",ToDay];
    NSString *subDate1 = [newDate substringWithRange:NSMakeRange(0, 4)];
    NSString *subDate2 = [newDate substringWithRange:NSMakeRange(5, 2)];
    NSString *subDate3 = [newDate substringWithRange:NSMakeRange(8, 2)];
    NSString *newDate2 = [NSString stringWithFormat:@"%@%@%@",subDate1,subDate2,subDate3];
    //明天时间
    NSTimeInterval secondsPerDay = 24*60*60;
    NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
    NSString *newTo = [NSString stringWithFormat:@"%@",tomorrow];
    NSString *subTo1 = [newTo substringWithRange:NSMakeRange(0, 4)];
    NSString *subTo2 = [newTo substringWithRange:NSMakeRange(5, 2)];
    NSString *subTo3 = [newTo substringWithRange:NSMakeRange(8, 2)];
    NSString *newTo2 = [NSString stringWithFormat:@"%@%@%@",subTo1,subTo2,subTo3];
    self.dateArr = @[newDate2,newTo2];
    [self reloadFortuneDetailData:self.dateArr[0]];
    
    self.blurImage = [[BlurImageView alloc] initWithFrame:kBounds];
    [self.view addSubview:_blurImage];
    [self setTitleView];
    [self setTableView];
    
    //详情界面的View
    self.fortuneView = [[FortuneDetailView alloc] initWithFrame:kBounds iconModel:self.model];
    [self.blurImage addSubview:self.fortuneView];
    
    [self setLeftAndRightButton];
}
//点击button出现的tableView
- (void)setTableView{
    self.arr = @[@" ",@"今日运势",@"明日运势",@"本周运势",@"本月运势",@"一年运势",@" "];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 0) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    
    UIImageView *vieww = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hehe.png"]];
    vieww.frame = kBounds;
    _tableView.backgroundView = vieww;
}
//titleView的Button点击方法
- (void)backBtn:(UIButton *)button
{
    if (self.isResult == NO) {
        [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, 0, kWidth, kHeight / 2);
        }];
        self.isResult = YES;
    }else{
        [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, 0, kWidth, 0);
        }];
        self.isResult = NO;
    }
}
//加载标题titleView
- (void)setTitleView{
    self.titleView = [[FortuneTitleView alloc] init];
    [_titleView.backBtn setTitle:@"宝宝" forState:(UIControlStateNormal)];
    [_titleView.backBtn setTitleColor:[UIColor purpleColor] forState:(UIControlStateNormal)];
    _titleView.backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 80, 0, 10);
    _titleView.backBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 40);
    self.navigationItem.titleView = _titleView.backBtn;
    //点击button加载的tableView的点击方法
    [self.titleView.backBtn addTarget:self action:@selector(backBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.isResult = NO;
}
//显示当前DetailView详情界面
- (void)setFortuneViewDetail{
    self.fortuneView.DModel = _Dmodel;
}
- (void)setWebViewUrl:(NSString *)str{
        if ([self.model.sname isEqualToString:@"白羊座"]) {
        self.urlWeek = [NSString stringWithFormat:kFortuneWeek,@"aries",str];
        }else if ([self.model.sname isEqualToString:@"金牛座"]){
        self.urlWeek = [NSString stringWithFormat:kFortuneWeek,@"taurus",str];
            
        }else if ([self.model.sname isEqualToString:@"双子座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"gemini",str];

        }else if ([self.model.sname isEqualToString:@"巨蟹座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"cancer",str];

        }else if ([self.model.sname isEqualToString:@"处女座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"virgo",str];

        }else if ([self.model.sname isEqualToString:@"天蝎座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"scorpio",str];

        }else if ([self.model.sname isEqualToString:@"射手座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"sagittarius",str];

        }else if ([self.model.sname isEqualToString:@"摩羯座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"capricorn",str];

        }else if ([self.model.sname isEqualToString:@"水瓶座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"aquarius",str];
;
        }else if ([self.model.sname isEqualToString:@"双鱼座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"pisces",str];

        }else if ([self.model.sname isEqualToString:@"天秤座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"libra",str];

        }else if ([self.model.sname isEqualToString:@"狮子座"]){
        _urlWeek = [NSString stringWithFormat:kFortuneWeek,@"leo",str];
        }
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 140, kWidth, kHeight - 140)];
    NSURL *webUrl = [NSURL URLWithString:_urlWeek];
    [self.webView loadRequest:[NSURLRequest requestWithURL:webUrl]];
    [self.view addSubview:self.webView];
    self.webView.delegate = self;
    self.webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ipad_bg"]];
    //重新加载tabelView
    [self.view addSubview:_tableView];
}
//开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView{
    self.alert = [[UIAlertView alloc] initWithTitle:@"正在加载..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
    self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
    [_act setFrame:CGRectMake(120, 48, 37, 37)];
    [_alert addSubview:_act];
    [_act startAnimating];
    [_alert show];
}
//完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_act stopAnimating];
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
    
}
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"加载失败  我特么还得让菊花消失");
    [_act stopAnimating];
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
}
//解析数据fortuneDetailArray
- (void)reloadFortuneDetailData:(NSArray *)dateStr{
        if ([self.model.sname isEqualToString:@"白羊座"]) {
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"aries",dateStr];
        }else if ([self.model.sname isEqualToString:@"金牛座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"taurus",dateStr];
        }else if ([self.model.sname isEqualToString:@"双子座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"gemini",dateStr];
        }else if ([self.model.sname isEqualToString:@"巨蟹座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"cancer",dateStr];
        }else if ([self.model.sname isEqualToString:@"处女座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"virgo",dateStr];
        }else if ([self.model.sname isEqualToString:@"天蝎座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"scorpio",dateStr];
        }else if ([self.model.sname isEqualToString:@"射手座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"sagittarius",dateStr];
        }else if ([self.model.sname isEqualToString:@"摩羯座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"capricorn",dateStr];
        }else if ([self.model.sname isEqualToString:@"水瓶座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"aquarius",dateStr];
        }else if ([self.model.sname isEqualToString:@"双鱼座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"pisces",dateStr];
        }else if ([self.model.sname isEqualToString:@"天秤座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"libra",dateStr];
        }else if ([self.model.sname isEqualToString:@"狮子座"]){
            self.urlToday = [NSString stringWithFormat:kFortuneDetailUrl,@"leo",dateStr];
        }
        //请求数据
    NSMutableURLRequest *requset = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlToday] cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:10.0];
    [requset setHTTPMethod:@"Get"];
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        _Dmodel = [[FortuneDetailModel alloc] init];
        [_Dmodel setValuesForKeysWithDictionary:dic];
        [self setFortuneViewDetail];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    if (indexPath.row == 0 && indexPath.row == 6) {
        cell.textLabel.text = @" ";
    }
    else{
    cell.textLabel.text = self.arr[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  self.webArr = @[@"week",@"month",@"year"];
    switch (indexPath.row) {
        case 1:
            [self reloadFortuneDetailData:self.dateArr[0]];
            [self.webView removeFromSuperview];
            self.tableView.frame = CGRectMake(0, 0, kWidth, 0);
            break;
        case 2:
            [self reloadFortuneDetailData:self.dateArr[1]];
            [self.webView removeFromSuperview];
            self.tableView.frame = CGRectMake(0, 0, kWidth, 0);
            break;
        case 3:
            [self setWebViewUrl:self.webArr[0]];
            self.tableView.frame = CGRectMake(0, 0, kWidth, 0);
            break;
        case 4:
            [self setWebViewUrl:self.webArr[1]];
            self.tableView.frame = CGRectMake(0, 0, kWidth, 0);
            break;
        case 5:
            [self setWebViewUrl:self.webArr[2]];
            self.tableView.frame = CGRectMake(0, 0, kWidth, 0);
            break;

        default:
            return;
            break;
    }
    self.isResult = NO;
    
}
//左右button
- (void)setLeftAndRightButton{
    //右边按钮
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,65,30)];
    [rightButton setTitle:@"星座壁纸" forState:(UIControlStateNormal)];
    [rightButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightButton addTarget:self action:@selector(searchPaper)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem= rightItem;
  
}

- (void)searchPaper{
    FortunePaperViewController *fortuneVC = [[FortunePaperViewController alloc] init];
    [self.navigationController pushViewController:fortuneVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
